const Food = require('../model/food.model')

//添加用户入库 ---增
class operationFood {
	async addFood(food_name, price, tag, address, detail, comments, mark) {
		const res = await Food.create({
			food_name,
			price,
			tag,
			address,
			detail,
			comments,
			mark
		})
		return res.dataValues

	}
	async getFoodInfo({ food_name, price, tag, address, mark }) {
		const whereOpt = {isOnShelf:1}
		food_name && Object.assign(whereOpt, { food_name })
		price && Object.assign(whereOpt, { price })
		tag && Object.assign(whereOpt, { tag })
		address && Object.assign(whereOpt, { address })
		mark && Object.assign(whereOpt, { mark })
		return Food.findAll({
			where: whereOpt
		});
	}

	async getFoodInfoById(id) {
		const res = await Food.findOne({
			// attributes: ['id', 'user_name', 'password', 'is_admin'],
			where: {
				id: id,
				isOnShelf:1
			}
		})
		return res
	}
	// async getFoodInfo({ food_name,price,tag,address,detail,comments,mark }) {
	// 	const whereOpt = {}

	// 	id && Object.assign(whereOpt, { id })
	// 	user_name && Object.assign(whereOpt, { user_name })
	// 	password && Object.assign(whereOpt, { password })
	// 	is_admin && Object.assign(whereOpt, { is_admin })

	// 	const res = await User.findOne({
	// 	  attributes: ['id', 'user_name', 'password', 'is_admin'],
	// 	  where: whereOpt,
	// 	})

	// 	return res ? res.dataValues : null
	//   }
	async modifyFood({ food_name, price, tag, address }, id) {
		const whereOpt = {}
		food_name && Object.assign(whereOpt, { food_name })
		price && Object.assign(whereOpt, { price })
		tag && Object.assign(whereOpt, { tag })
		address && Object.assign(whereOpt, { address })
		Food.update(whereOpt, { 
			where: { 
				id,
				isOnShelf:1
			} 
		});
	}
	async getAllFoodInfo() {
		return Food.findAll({
			where:{
				isOnShelf:1
			}
		})
	}
	async deleteFood(id){
		Food.update({
			isOnShelf:false
		},{
			where:{id}
		})
	}
}
module.exports = new operationFood()